package com.example.nna.voz;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.UUID;

import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.graphics.Bitmap;

import com.example.nna.util.UserInfo;

public class ParserUserCP {
	private String m_url;
	private Document doc;
	private Element mainElement;
	Response res;
	private ArrayList<containPage2> ListContains;
	private UserInfo mUser;
	public String mIndexPage;
	public String Urlpage;
	public String Urlprev;
	public String Urlnext;
	public String Urlfirst;
	public String Urllast;
	public int curPage;
	private int lastPage;
	public ParserUserCP(String m_url ){
		this.m_url = m_url;
		ListContains = new ArrayList<containPage2>();
		mUser = new UserInfo();
	}
    public int parser(){
		try {
			if(mUser.cookies() == null){
				doc = Jsoup.connect(m_url).timeout(60000).get();
			}else{
				doc = Jsoup.connect(m_url)
						.timeout(60000)
						.cookies(mUser.cookies())
						.get();
			}
			if(doc.select("td[class*=tcat]").first().text().equals("vBulletin Message")){
				return 1;
			}else{
				mainElement = doc.select("form[name = form]").first();
				if(mainElement!= null){	
					ListContains.clear();
					PaserExecute();
				}
				return 0;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
    }
    public int paserSubscribe(){
		try {
			doc = Jsoup.connect(m_url)
					.timeout(60000)
					.cookies(mUser.cookies())
					.get();
			
			if(doc.select("td[class*=tcat]").first().text().equals("vBulletin Message")){
				return 1;
			}else{
				mainElement = doc.select("table[class = tborder]:has(thead)").first();
				if(mainElement!= null){	
					ListContains.clear();
					PaserExecute();
				}
				return 0;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
    }
    public String getIndexPage(){
    	return mIndexPage;
    }
    public ArrayList<containPage2> getContain(){
    	return ListContains;
    }
    public containPage2 getContain(int index){
    	return ListContains.get(index);
    }
    private void parsePage(){
    	Element elePage = doc.select("div[class=pagenav").first();
    	Urlfirst = null;
    	Urllast = null;
    	Urlnext = null;
    	Urlpage = null;
    	Urlprev = null;
    	if(elePage!=null){
    		mIndexPage = elePage.select("td[class=vbmenu_control]").text();
    		curPage = Integer.parseInt(mIndexPage.split(" ")[1]);
    		lastPage = Integer.parseInt(mIndexPage.split(" ")[3]);
    		if(m_url.contains("&page=") != true){
    			m_url = m_url.concat("&page=1");
    		}
    		Urlnext = m_url.substring(0,m_url.length()-1).concat(String.valueOf(curPage+1));
    		
    		Urlprev = m_url.substring(0,m_url.length()-1).concat(String.valueOf(curPage-1));
    		if(Urlprev.contains("&page=0")){
    			Urlprev = Urlprev.split("&page")[0];
    		}
    		Urlfirst = m_url.split("&page")[0];
    		Urllast = Urlfirst.concat("&page=" + String.valueOf(lastPage));
    	}
    }
    private void PaserExecute(){
    	Element eleTag1,eleTitle,eleLastPost,eleFirstNewPost = null,eleThreadstatusicon;
    	containPage2 mContain;
		String text1 = null,text2=null,text1_url1=null,text2_url2=null,text3_url=null;
		Bitmap bitmap = null;
		parsePage();
		Elements eleTag1s = mainElement.select("tr:has([id*=td_thread])");
		Iterator<Element> iteTag1 = eleTag1s.iterator();
		ListContains.add(new containPage2(null,null,null,null,null,null,null));
		while(iteTag1.hasNext()){
			eleTag1 = iteTag1.next();
			eleThreadstatusicon = eleTag1.select("img[id*=thread_statusicon").first();
			if(eleThreadstatusicon!= null){
				bitmap = taskLoadImage.DownloadImage(eleThreadstatusicon.attr("src"));
			}
	     	eleTitle = eleTag1.select("td[id *= td_threadtitle]").first();
			if(eleTitle != null){
				eleFirstNewPost = eleTitle.select("a[id *= thread_gotonew]").first();
				eleTitle = eleTitle.select("a[id *= thread_title]").first();
			}
			if(eleTitle != null){
				text1 = eleTitle.text();
				text1_url1 = eleTitle.attr("href");
			}
			if(eleFirstNewPost != null){
				text2_url2 = eleFirstNewPost.attr("href");
			}
			eleLastPost = eleTag1.select("td[class = alt2]:has(span)").first();
			if(eleLastPost != null){
				text2 = eleLastPost.text();
				text3_url = eleLastPost.select("a:has(img)").attr("href");
			}
			mContain = new containPage2(text1,
					text2, 
					text3_url,
					null,
					bitmap,
					text1_url1, 
					text2_url2);
			if((eleTitle != null) && (eleTitle.hasAttr("style")))
				mContain.setSticky(true);
			ListContains.add(mContain);

		}
    }
}
